﻿package  shapes.shapes2D.distribution
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	import shapes.shapes2D.Dot;
	import utils.GeomUtils;
	/**
	 * https://malenkov.dev.java.net/20090226/hexagon.java
	 * 
	 * 
	 * @author Nicolas Barradeau
	 */
	public class Hexagonal extends Sprite
	{
		
		static private var W:Number = 50;
		static private var R:Number = W / 2.0;
		static private var S:Number = R / Math.cos(Math.PI / 6.0);
		static private var L:Number = S / 2.0;
		static private var H:Number = S + L;

		static private var _i:int;
		static private var _j:int;

		static private var points:Vector.<Point>;

		public function Hexagonal() 
		{
		}
		
		static public function distribute( size:Number = 25, width:Number = 250, height:Number = 250, jitter:Number = 0 ) :  Vector.<Point>
		{
			
			W = size;
			R = W / 2.0;
			S = R / Math.cos(Math.PI / 6.0);
			L = S / 2.0;
			H = S + L;
			
			points = new Vector.<Point>();
			
			getPoints( width, height);
			
			fRemoveDup( points );
			
			if( jitter != 0 ) jitterPoints( jitter );
			
			return points;
			
		}
		
		static private function fRemoveDup(ac: Vector.<Point>) : void
		{
			var i:int, j : int;
			for (i = 0; i < ac.length; i++)
			{
				for (j = 0; j < ac.length; j++)
				{
					
					if ( ac[i].equals( ac[j] ) )
					{
						ac.splice(j, 1);
					}
				}
			}
		}
		
		static private function jitterPoints( radius:Number ):void
		{
			var rad:Number;
			var angle:Number;
			for each( var p:Point in points )
			{
				
				rad = radius * Math.random();
				angle = ( 2 * Math.PI ) * Math.random();
				p.x += rad * Math.cos(angle);
				p.y += rad * Math.sin(angle);
				
			}
		}
		
		static private function getPoints(  width:Number = 250, height:Number = 120 ):void
		{
			var i:int = -1;
			var j:int = -1;

			var y:Number = -L;
			while (y < height)
			{
				var x:Number = i * W + R - R * j;
				while (x < width) 
				{
					points.push( 
									new Point( int(x), int(y - S) ),
									new Point( int(x + R), int(y - L) ),
									new Point( int(x + R), int(y + L) ),
									new Point( int(x), int(y + S ) ),
									new Point( int(x - R), int(y + L ) ),
									new Point( int(x - R), int(y + L ) )
									
								);
					
					
					
					var distance:Number = 0.0;
					if ((_i != i) || (_j != j)) 
					{
						var di:int = Math.abs(_i - i);
						var dj:int = Math.abs(_j - j);
						var flag:Boolean = ((_i <= i) && (_j >= j)) || ((_i >= i) && (_j <= j));
						distance = W * Number( flag ? di + dj : Math.max(di, dj));
					}
					
					x += W;
					i++;
				}
				y += H;
				j++;
				i = j / 2;
			}
		}
	}

}
/*
stage.align = 'TL';

var pts:Array = [];


var size:Number = 60;


var lx:int = 16;

var py:int = 0;


var ratioX:Number = .865;
var ratioY:Number = .75;

var offsetX:Number = 0//( size/4 );
var offsetY:Number =  0;


function hexaTiles( e:Event ):void
{
	
	graphics.clear();

	var X:int = -1;
	var Y:int = -1;
	var px:int = 1;
	for (var i:int= 0; i < 200; i++)
	{
		
		var tlx:int = ( px == 0 ) ? lx : ( lx+1 );
		X++;
		if( X % tlx == 0  )
		{
			
			X = 1;
			Y++;
			px = ( px == 0 ) ? 1 : 0; 
			
		}
		
		
		var dx:Number = offsetX + ( ( X * size * ratioX ) + ( px * ( size / 2 * ratioX ) ) );
		dx -= ( px == 1 ) ? ( size * ratioX ) : 0;
		
		var dy:Number = offsetY + (( Y * size * ratioY ) + size / 2);
		
		graphics.lineStyle( 0, 0, 1);
		drawHexagon( graphics, dx, dy, size / 2 );
		
	}
}
addEventListener( Event.ENTER_FRAME, hexaTiles );


function drawHexagon( graphics:Graphics, x:Number = 0, y:Number = 0, size:int = 10 ):void
{
	
	var i:int = 360;
	graphics.moveTo( x + Math.cos( -30 / 180  * Math.PI ) * size, y + Math.sin( -30 / 180  * Math.PI ) * size  );
	while( i )
	{
		var px:Number = Math.cos( ( i + 30 ) / 180 * Math.PI ) * size;
		var py:Number = Math.sin( ( i + 30 ) / 180 * Math.PI ) * size;
		graphics.lineTo( x + px, y + py );
		i -= 60;
	}
	graphics.lineTo( x + Math.cos( 30 / 180  * Math.PI ) * size, y + Math.sin( 30 / 180  * Math.PI ) * size  );
	
}



trace( '----------' );
//trace( 17.32050807568878 /20 );
trace( '----------' );

*/




/*
    private static final double W = 40.0;
    private static final double R = W / 2.0;
    private static final double S = R / Math.cos(Math.PI / 6.0);
    private static final double L = S / 2.0;
    private static final double H = S + L;

    private int i;
    private int j;

    private final int[] x = new int[6];
    private final int[] y = new int[this.x.length];

    { addMouseMotionListener(this); }

    @Override
    public void distribute(Graphics g) {
        FontMetrics fm = g.getFontMetrics();

        double width = R + (double) getWidth();
        double height = S + (double) getHeight();

        int i = -1;
        int j = -1;

        double y = -L;
        while (y < height) {

            double x = (double) i * W + R - R * (double) j;
            while (x < width) {

                this.x[0] = (int) (x);
                this.y[0] = (int) (y - S);

                this.x[1] = (int) (x + R);
                this.y[1] = (int) (y - L);

                this.x[2] = this.x[1];
                this.y[2] = (int) (y + L);

                this.x[3] = this.x[0];
                this.y[3] = (int) (y + S);

                this.x[4] = (int) (x - R);
                this.y[4] = this.y[2];

                this.x[5] = this.x[4];
                this.y[5] = this.y[1];

                double distance = 0.0;
                if ((this.i != i) || (this.j != j)) {
                    int di = Math.abs(this.i - i);
                    int dj = Math.abs(this.j - j);
                    boolean flag = ((this.i <= i) && (this.j >= j)) || ((this.i >= i) && (this.j <= j));
                    distance = W * (double) (flag ? di + dj : Math.max(di, dj));
                }
                boolean fill = distance <= W * 8;
                if (fill) {
                    float c = (float) (distance / W / 10.0);
                    g.setColor(new Color(c, c, c));
                    g.fillPolygon(this.x, this.y, this.y.length);
                    g.setColor(getBackground());
                }
                else {
                    g.drawPolygon(this.x, this.y, this.y.length);
                }

                String s1 = i + " ; " + j;
                String s2 = Integer.toString((int) distance);

                int sy = (int) y - fm.getDescent();
                g.drawString(s1, (int) (x - (double) fm.stringWidth(s1) / 2.0), sy);
                g.drawString(s2, (int) (x - (double) fm.stringWidth(s2) / 2.0), sy + fm.getHeight());

                if (fill) {
                    g.setColor(getForeground());
                }
                x += W;
                i++;
            }
            y += H;
            j++;
            i = j / 2;
        }
    }
*/